<?php
/*
  $Id: administrators_grid.php $
  Mefobe Cart Solutions
  http://www.mefobemarket.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

?>

Toc.roles.PermissionsGrid = function(config) {

  config = config || {};
  config.autoHeight = true;
  config.title = 'Permissions';
  config.border = true;
  config.viewConfig = {emptyText: TocLanguage.gridNoRecords};

  config.ds = new Ext.data.Store({
    url: Toc.CONF.CONN_URL,
    baseParams: {
      module: 'categories',
      action: 'list_role_permissions',
      roles_id : config.rolesId
    },
    reader: new Ext.data.JsonReader({
      root: Toc.CONF.JSON_READER_ROOT,
      totalProperty: Toc.CONF.JSON_READER_TOTAL_PROPERTY,
      id: 'categories_id'
    },
    [
      'categories_id',
      'administrators_id',
      'roles_id',
      'categories_name',
      'can_read',
      'can_write',
      'can_modify',
      'can_publish'
    ]),
    autoLoad: true
  });

  render = function(status) {
    if(status == 1) {
      return '<img class="img-button" src="images/icon_status_green.gif" />&nbsp;<img class="img-button btn-status-off" style="cursor: pointer" src="images/icon_status_red_light.gif" />';
    }else {
      return '<img class="img-button btn-status-on" style="cursor: pointer" src="images/icon_status_green_light.gif" />&nbsp;<img class="img-button" src= "images/icon_status_red.gif" />';
    }
  };

  config.listeners = {
    activate : function(panel) {
      //this.load();
    },
    show : function(comp) {
    },
    beforeshow : function(comp) {
    }
  };

  config.cm = new Ext.grid.ColumnModel([
    {
      id: 'categories_name',
      header: 'Page',
      dataIndex: 'categories_name'
    },
    { header: 'Lecture', align: 'center', renderer: render, dataIndex: 'can_read'},
    { header: 'Ecriture', align: 'center', renderer: render, dataIndex: 'can_write'},
    { header: 'Modification', align: 'center', renderer: render, dataIndex: 'can_modify'},
    { header: 'Publication', align: 'center', renderer: render, dataIndex: 'can_publish'}
  ]);
  config.autoExpandColumn = 'categories_name';

  var thisObj = this;

  Toc.roles.PermissionsGrid.superclass.constructor.call(this, config);
};

Ext.extend(Toc.roles.PermissionsGrid, Ext.grid.GridPanel, {

  refreshGrid: function (rolesId) {
    console.log('refreshing grid........this');
    console.dir(this);
    var store = this.getStore();

    store.baseParams['roles_id'] = rolesId;
    store.load();
  },

  setCategoriesId: function (categoriesId) {
    this.rolesId = rolesId;
  },

  onClick: function(e, target) {
    console.log('clicked......e');
    console.dir(e);
    console.log('clicked......target');
    console.dir(target);
    console.log('clicked......this');
    console.dir(this);
    console.log('clicked......position : ' + this.getPosition());
    console.dir(this);

    var t = e.getTarget();
    var v = this.view;
    var row = v.findRowIndex(t);
    var action = false;

    if (row !== false) {
      var btn = e.getTarget(".img-button");

      if (btn) {
        action = btn.className.replace(/img-button btn-/, '').trim();
      }

      if (action != 'img-button') {
        var categories_id = this.getStore().getAt(row).get('categories_id');
        var roles_id = this.getStore().getAt(row).get('roles_id');
        var module = 'setPermission';

        switch(action) {
          case 'status-off':
          case 'status-on':
            flag = (action == 'status-on') ? 1 : 0;
            this.onAction(module, categories_id,roles_id,permission, flag);

            break;
        }
      }
    }
  },

  onClick: function(e, target) {
    var t = e.getTarget();
    var v = this.view;
    var row = v.findRowIndex(t);
    var col = v.findCellIndex(t);
    var action = false;

    if (row !== false) {
      if (col > 0) {
        var record = this.getStore().getAt(row);
        var flagName = this.getColumnModel().getDataIndex(col);
        this.fireEvent('selectchange', record);
      }

      var btn = e.getTarget(".img-button");

      if (btn) {
        action = btn.className.replace(/img-button btn-/, '').trim();
        var categories_id = this.getStore().getAt(row).get('categories_id');
        var roles_id = this.getStore().getAt(row).get('roles_id');
        var module = 'setPermission';

        switch(action) {
          case 'status-off':
          case 'status-on':
            flag = (action == 'status-on') ? 1 : 0;
            this.setPermission(module, categories_id,roles_id,flagName, flag);
            break;
        }
      }
    }
  },

  setPermission: function(action, categories_id,roles_id,permission, flag) {
    Ext.Ajax.request({
      url: Toc.CONF.CONN_URL,
      params: {
        module: 'categories',
        action: action,
        categories_id: categories_id,
        roles_id: roles_id,
        flag: flag,
        permission : permission
      },
      callback: function(options, success, response) {
        var result = Ext.decode(response.responseText);

        if (result.success == true) {
          var store = this.getStore();
          store.load();
          //store.getById(categories_id).set(permission, flag);
          //store.commitChanges();

          //this.owner.app.showNotification({title: TocLanguage.msgSuccessTitle, html: result.feedback});
        }
      },
      scope: this
    });
  }
});